package com.qf.controller;

import com.qf.entity.User;
import com.qf.entity.custom.*;
import com.qf.mapper.UserModule;
import com.qf.tools.JavaWebToken;
import com.qf.tools.ResultEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

/**
 * @author tanchangming
 * @Date 2020.7.22
 * @Version 1.0 用户模块功能实现
 */
@RestController
@RequestMapping("/userModuleController")
public class UserModuleController {
    @Autowired
    private UserModule userModule;
    /**
     * @author tanchangming
     * @method selectNameAndImageByUserId 查询用户表中用户的名字和头像,request:get
     * @param user JSON类型的user
     * @return 将得出来的user对象转成JSON类型并返回
     * @description 根据用户id查询用户表信息，单表查询，token验证
     */
    @RequestMapping(value = "/selectNameAndImageByUserId", method = RequestMethod.POST)
    public ResultEntity selectNameAndImageByUserId(@RequestBody User user) {
        //ResultEntity
     if(user != null ){
          String token = user.getToken();
          Map<String,Object> map = JavaWebToken.parserJavaWebToken(token);
          Integer userId = (Integer)map.get("userId");
          if(userId != null) {
              User user1 = userModule.selectNameAndImageByUserId(userId);
              System.out.println(user1);
              return ResultEntity.success(user1);
              } else {
              return ResultEntity.faild(false);
                }
          } else  {
         return ResultEntity.faild(false);
     }
   /*  User user1 = userModule.selectNameAndImageByUserId(user.getUserId());
     return user1;*/
    }

    /**
     * @author tanchangming
     * @method  ColUserShare 用户收藏功能
     * @param user json类型
     * @return 返回一个json形式的对象
     * @description 多表联查（ col表，user表，晒单表）前端传当前登录的用户id，后端根据id查询用户表和晒单表信息；token验证
     */
    @RequestMapping(value = "/ColUserShare",method = RequestMethod.POST)
    public ResultEntity selectColUserShare(@RequestBody User user){
        if(user != null ){
            String token = user.getToken();
            Map<String,Object> map = JavaWebToken.parserJavaWebToken(token);
            Integer userId = (Integer)map.get("userId");
            if(userId != null) {
                List<ColUserSha> colUserSha = userModule.selectColUserShare(userId);
                return ResultEntity.success(colUserSha);
            } else {
                return ResultEntity.faild(false);
            }
        } else {
            return ResultEntity.faild(false);
        }

    }

    /**
     * @author tanchangming
     * @param user json类型
     * @methor UserEvaShop 查询用户的个人评论信息
     * @return 返回值 userEvaShopCustom json类型数据
     * @description 多表查询（用户表，评论表，商铺表）；token验证
     */
  @RequestMapping(value = "/UserEvaShop", method = RequestMethod.POST)
    public ResultEntity UserEvaShop(@RequestBody User user) {
        if(user != null ){
            System.out.println("user not null");
            String token = user.getToken();
            Map<String,Object> map = JavaWebToken.parserJavaWebToken(token);
            Integer userId = (Integer)map.get("userId");
            if(userId != null) {
                System.out.println("userId not null");
                UserEvaShopCustom userEvaShopCustom = userModule.selectUserEvaShop(userId);
                System.out.println(userEvaShopCustom);
                return ResultEntity.success(userEvaShopCustom);
            } else {
                System.out.println("userId is null");
                return ResultEntity.faild(false);
            }
        } else {
            System.out.println("user is null");
            return ResultEntity.faild(false);
        }
    }


    /**
     * @author tanchangming
     * @param user json类型
     * @method selectUserBroShop 查询用户个人浏览信息，返回json数据
     * @return 返回json类型数据
     * @description 多表查询（user表，浏览表，shop表）；token
     */
    @RequestMapping(value = "/UserBroShop",method = RequestMethod.POST)
    public ResultEntity selectUserBroShop(@RequestBody User user){
        if(user != null ){
            System.out.println("user not null");
            String token = user.getToken();
            Map<String,Object> map = JavaWebToken.parserJavaWebToken(token);
            Integer userId = (Integer)map.get("userId");
            if(userId != null) {
                System.out.println("userId not null");
                UserBroShopCustom userBroShopCustom = userModule.selectUserBroShop(userId);
                return ResultEntity.success(userBroShopCustom);
            } else {
                System.out.println("userId is null");
                return ResultEntity.faild(false);
            }
        } else {
            System.out.println("user is null");
            return ResultEntity.faild(false);
        }
    }



    /**
     * @author tanchangming
     * @method selectNameAndImageByUserId 根据用户id查询个人优惠劵信息
     * @param  user json类型
     * @return 返回coupon类型数据转成json
     * @description 多表查询;token
     */
    @RequestMapping(value = "/selectCouponByUserId", method = RequestMethod.POST)
    public ResultEntity selectCouponByUserId(@RequestBody User user) {
       /* int id = user.getUserId();
        UserToCouponCou userToCouponCou = userModule.selectUserCoupons(id);
        return userToCouponCou;*/
        if(user != null ){
            System.out.println("user not null");
            String token = user.getToken();
            Map<String,Object> map = JavaWebToken.parserJavaWebToken(token);
            Integer userId = (Integer)map.get("userId");
            if(userId != null) {
                System.out.println("userId not null");
                UserCustom userCustom = userModule.selectUserCoupons(userId);
                return ResultEntity.success(userCustom);
            } else {
                System.out.println("userId is null");
                return ResultEntity.faild(false);
            }
        } else {
            System.out.println("user is null");
            return ResultEntity.faild(false);
        }
    }

    /**
     * @return 返回的user转成JSON
     * @describetion 前端传当前用户id，后端查询余额信息返回。
     * @method selectNameAndImageByUserId 根据用户id查询user表余额信息
     */
    @RequestMapping(value = "/selectBalanceByUserId", method = RequestMethod.POST)
    public ResultEntity selectBalanceByUserId(@RequestBody User user) {
        /*int id = user.getUserId();
        User user1 = userModule.selectBalanceByUserId(id);
        return user1;*/
        if(user != null ){
            System.out.println("user not null");
            String token = user.getToken();
            Map<String,Object> map = JavaWebToken.parserJavaWebToken(token);
            Integer userId = (Integer)map.get("userId");
            if(userId != null) {
                System.out.println("userId not null");
                User user1 = userModule.selectBalanceByUserId(userId);
                return ResultEntity.success(user1);
            } else {
                System.out.println("userId is null");
                return ResultEntity.faild(false);
            }
        } else {
            System.out.println("user is null");
            return ResultEntity.faild(false);
        }
}}


